package com.heima.cn;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;

public class ServletDemo02 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //根据名称获取数据.
        String username = req.getParameter("username");
        System.out.println(username);
        String password = req.getParameter("password");
        System.out.println(password);
        System.out.println("----------------------------");
        //根据名称获取所有的数据
        String[] hobbies = req.getParameterValues("hobby");
        for (String hobby:hobbies){
            System.out.println(hobby);
        }
        System.out.println("----------------------------");
        //获取所有的名称
        Enumeration<String> names = req.getParameterNames();
        while(names.hasMoreElements()){
            String name = names.nextElement();
            String parameter = req.getParameter(name);
            System.out.println(parameter);
        }
        //获取所有的参数的键值对
        Map<String, String[]> parameterMap = req.getParameterMap();
        for (String key: parameterMap.keySet()){
            String[] values = parameterMap.get(key);
            System.out.println(values);
            for (String value:values){
                System.out.println(value);
            }
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
